//
//  UIView+LBEmptyData.m
//  jslj
//
//  Created by 杨庆人 on 2017/9/28.
//  Copyright © 2017年 广东联结电子商务有限公司. All rights reserved.
//

#import "UIView+LBEmptyData.h"
#import <Masonry/Masonry.h>
#import <objc/runtime.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import "LBEmptyDataSystemView.h"
#import "UIScrollView+LBEmptyDataSet.h"
#import "LBEmptyDataBusinessView.h"

@interface UIView ()
@property (nonatomic,strong)LBEmptyDataSystemView *emptyDataView;
@end

@implementation UIView (LBEmptyData)

/**
 OC的分类允许添加属性，但不会自动生成getter、setter方法
 */

static const char emptyDataViewkey;

- (UIView *)emptyDataView {
    return objc_getAssociatedObject(self, &emptyDataViewkey);
}

- (void)setEmptyDataView:(UIView *)emptyDataView {
    objc_setAssociatedObject(self, &emptyDataViewkey, emptyDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}



// 需要登录，重新登录block
- (void)lb_setEmptyDataWithNeedLogin:(LBEmptyDataBlock)loginBlock {
    [self show:LBEmptyDataTypeNeedLogin block:loginBlock title:nil detail:nil customBtnName:nil];
}

// 没有数据，显示文案
- (void)lb_setEmptyNoData:(NSString *)title {
    [self show:LBEmptyDataTypeNoData block:nil title:title detail:nil customBtnName:nil];
}
- (void)lb_setEmptyNoData:(NSString *)title detail:(NSString *)detail {
    [self show:LBEmptyDataTypeNoData block:nil title:title detail:detail customBtnName:nil];
}
- (void)lb_setEmptyNoData:(NSString *)title reloadBlock:(LBEmptyDataBlock)block {
    [self show:LBEmptyDataTypeNoData block:block title:title detail:nil customBtnName:nil];
}
- (void)lb_setEmptyNoData:(NSString *)title detail:(NSString *)detail reloadBlock:(LBEmptyDataBlock)block {
    [self show:LBEmptyDataTypeNoData block:block title:title detail:detail customBtnName:nil];
}
- (void)lb_setEmptyNoData:(NSString *)title detail:(NSString *)detail customBtnName:(NSString *)buttonName block:(LBEmptyDataBlock)block {
    [self show:LBEmptyDataTypeNoData block:block title:title detail:detail customBtnName:buttonName];
}

/**
 没有数据，不附带图
 */
- (void)lb_setEmptyDataNoImageWithTitle:(NSString *)title {
    [self show:LBEmptyDataTypeNoDataNoImage block:nil title:title detail:nil customBtnName:nil];
}
- (void)lb_setEmptyDataNoImageWithTitle:(NSString *)title detail:(NSString *)detail {
    [self show:LBEmptyDataTypeNoDataNoImage block:nil title:title detail:detail customBtnName:nil];
}

- (void)show:(LBEmptyDataSystemType)type block:(LBEmptyDataBlock)block title:(NSString *)title detail:(NSString *)detail customBtnName:(NSString *)buttonName{
    if (self.emptyDataView) {
        [self.emptyDataView removeFromSuperview];
        self.emptyDataView = nil;
    }

    // 定制customView
    self.emptyDataView = [[LBEmptyDataSystemView alloc] initWithLBEmptyDataType:type title:title detail:detail block:block customBtnName:buttonName];

    if ([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]]) {

        UIScrollView *scrollView = (UIScrollView *)self;
        scrollView.emptyDataSetSource = self;
        scrollView.emptyDataSetDelegate = self;
    }
    else {
        [self addSubview:self.emptyDataView];
        [self.emptyDataView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
    }
}


// 业务空白页
- (void)lb_setEmptyDataWithBsImage:(UIImage *)image title:(NSString *)title detail:(NSString *)detail button:(NSArray <NSString *>*)button block:(LBEmptyDataIndexBlock)block {
    if (self.emptyDataView) {
        [self.emptyDataView removeFromSuperview];
        self.emptyDataView = nil;
    }
    
    // 定制customView
    self.emptyDataView = [[LBEmptyDataBusinessView alloc] initWithBsImage:image title:title detail:detail button:button block:block];
    
    if ([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]]) {
        
        UIScrollView *scrollView = (UIScrollView *)self;
        scrollView.emptyDataSetSource = self;
        scrollView.emptyDataSetDelegate = self;
    }
    else {
        [self addSubview:self.emptyDataView];
        [self.emptyDataView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
    }
}

/**
 自定义view
 */
- (void)lb_setCustomView:(__kindof UIView *)customView {
    
    if (self.emptyDataView) {
        [self.emptyDataView removeFromSuperview];
        self.emptyDataView = nil;
    }
    
    // 定制customView
    self.emptyDataView = customView;
    
    if ([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]]) {
        
        UIScrollView *scrollView = (UIScrollView *)self;
        scrollView.emptyDataSetSource = self;
        scrollView.emptyDataSetDelegate = self;
    }
    else {
        [self addSubview:self.emptyDataView];
        [self.emptyDataView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
    }
}

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView{

    return self.emptyDataView;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView {
    [UIView animateWithDuration:0.4 animations:^{
        scrollView.contentOffset = CGPointZero;
    }];
}

/**
 设置customView的约束
 
 @param customView
 */
- (void)setConstraintForEmptyDataSet:(UIView *)customView {
    [customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}

/**
 设置title、detail颜色
 */
- (void)lb_setEmptyThemeColor:(UIColor *)color {
    if ([self.emptyDataView isKindOfClass:[LBEmptyDataSystemView class]]) {
        LBEmptyDataSystemView *view = self.emptyDataView;
        view.titleLabel.textColor = color;
        view.detailLabel.textColor = color;
    }
}

- (void)lb_removeEmptyData {
    if (self.emptyDataView) {
        [self.emptyDataView removeFromSuperview];
        self.emptyDataView = nil;
    }
}

@end
